/*
 * @Author: yt 2399493770@qq.com
 * @Date: 2024-04-25 23:16:38
 * @LastEditors: yt 2399493770@qq.com
 * @LastEditTime: 2024-04-25 23:46:22
 * @Description: 
 */
import { BasicsResult } from "./BasicsResult";

export { errorHandler };

function errorHandler(err: any) {
  if (typeof err === "string") {
    // custom application error
    const is404 = err.toLowerCase().endsWith("not found");
    const status = is404 ? 404 : 400;

    return BasicsResult.error(status, err);
  }

  if (err.name === "JsonWebTokenError") {
    // jwt error - delete cookie to auto logout
    return BasicsResult.error(401, "token无效");
  }

  // 默认 500 返回值
  return BasicsResult.error(500, err.message);
}
